package com.edu.dgut.service;

import com.edu.dgut.entity.classificationEntity;
import com.edu.dgut.entity.goodsEntity;
import com.edu.dgut.dao.classificationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//分类表业务
@Service
public class classificationService {

    @Autowired
    private classificationRepository classification;

    //新建分类
    @ResponseBody
    public Map<String,Object> newcategory(String category){
        Map<String, Object> map = new HashMap<String, Object>();
        classificationEntity new_classification = new classificationEntity();
        List<classificationEntity> old_classification = classification.findByCategoryLike("%"+category+"%");
        //若分类名不存在，则添加，已存在则返回0
        if (old_classification.size() == 0) {
            new_classification.setCategory(category);
            new_classification = classification.save(new_classification);
            if(new_classification != null) {
                map.put("status", 1);
            }else {
                map.put("status", 0);
            }
        } else {
            map.put("status", 0);
        }
        return map;
    }

    //添加或修改分类信息,修改时一次修改一个商品的分类
    @ResponseBody
    public Map<String,Object> saveOrModify(classificationEntity new_classification) {
        Map<String,Object> map = new HashMap<String,Object>();
        int i = 0;
        String category = new_classification.getCategory();
        List<classificationEntity> old_classification = classification.findByCategoryLike(category);
        goodsEntity goods1 = new goodsEntity();
        goodsEntity goods2 = new goodsEntity();
        List<goodsEntity> new_goods = new ArrayList<>();
        //若分类信息不存在，则添加，已存在则修改
        if(old_classification.size() != 0) {
            //将新旧商品都加入new_goods然后保存到new_classification的List<goods>中
            goods2 = new_classification.getGoods().get(0);
            new_goods.add(goods2);
            do {
                if(old_classification.get(0).getGoods().size() !=0) {
                    goods1 = old_classification.get(0).getGoods().get(i++);
                    if (goods1 !=null && goods1.getGoodsId() != goods2.getGoodsId()) {
                        new_goods.add(goods1);
                    }
                }
            }while(old_classification.get(0).getGoods().size()>i);
            new_classification.setId(old_classification.get(0).getId());
            new_classification.setGoods(new_goods);
        }
        new_classification = classification.save(new_classification);
        if (new_classification != null) {
            map.put("status", 1);
        } else {
            map.put("status", 0);
        }
        return map;
    }

    //删除一个商品的类别信息
    @ResponseBody
    public Map<String,Object> delete(classificationEntity delete_classification) {
        int i = 0;
        String category = delete_classification.getCategory();
        int goods_id = delete_classification.getGoods().get(0).getGoodsId();
        List<classificationEntity> old_classification = classification.findByCategoryLike(category);
        classificationEntity new_classification = new classificationEntity();
        goodsEntity goods = new goodsEntity();
        List<goodsEntity> new_goods = new ArrayList<>();
        //将除了要删除的商品加入new_goods
        if(old_classification.size() != 0) {
            do {
                goods = old_classification.get(0).getGoods().get(i++);
                if (goods != null && goods.getGoodsId() != goods_id) {
                    new_goods.add(goods);
                }
            }while(old_classification.get(0).getGoods().size()>i);
            new_classification.setId(old_classification.get(0).getId());
            new_classification.setCategory(category);
            new_classification.setGoods(new_goods);
        }
        new_classification = classification.save(new_classification);
        Map<String,Object> map = new HashMap<String,Object>();
        if(new_classification != null) {
            map.put("status", 1);
        }else {
            map.put("status", 0);
        }
        return map;
    }
}
